﻿(function ($) {
    $.fn.checkboxToCols = function () {
        var j = 0;
        $(this).find("input[type=checkbox]").each(function () {
            $this = $(this);
            if (this.checked) {
                var boxname = $this.attr("name").replace(".", "_") + "spanCheckToMutilCols";
                var $box = $("<span id='" + boxname + "'></span>");
                $("#" + boxname).remove();
                var columns = $this.attr("columns") || "";
                var prefix = $this.attr("prefix") || "";
                if (prefix) prefix = prefix + "[" + j + "].";
                var coljson = columns.length > 0 ? eval("(" + columns + ")") : eval([]);
                if (coljson.length > 0) {
                    for (var i = 0; i < coljson.length; i++) {
                        for (var key in coljson[i]) {
                            var $target = $("<input type='hidden' id='" + prefix + key + "' name='" + prefix + key + "' value='" + coljson[i][key] + "' />");
                            $target.appendTo($box);
                        }
                    }
                    j += 1;
                    $this.after($box);
                }
            }
            $this.click(function () { $(this).checkboxToCols(); });
        });
    };
})(jQuery);